<?php namespace Home\Controller; 
use Think\Controller; 
class WenController extends CommonController
{   
	//问题发布首页
	public function index(){
		$cate=D('Category')->GetCate();
		$this->assign('cate',$cate);
        $question =D('Verify')->getOneVerify();
        $this->assign('question',$question);
		$this->display();

	}

	//发布提问
	public function add()
	{

		//POST 	获取人类验证		
		$answer = $_POST['answer'];
		//判断人类验证
		if ($answer!=$_SESSION['answer']) {
			$arr = array(
    			'error'=>1,
    			'info'=>'人类验证失败，请重新输入！'
    			);
    		echo json_encode($arr);
    		exit;
		}
		$_SESSION['answer'] ='';

		
		//获取KISS
		$kiss=$_POST['kiss_num'];

		$id=$_SESSION['id'];
		
		$sql="select kiss from user where id='{$id}'";
		$res=M('user')->query($sql);
		$res=current($res);
		//判断KISS
		if ($kiss>$res['kiss']) {
			$arr = array(
    			'error'=>1,
    			'info'=>'您的飞吻不够，问题提交失败！'
    			);
    		echo json_encode($arr);
    		exit;
		}

		//获取POST表单所有信息
		$data=$_POST;
		//增加时间
		$data['create_time']=time();
		$data['updata']=time();
		$data['uid']=$_SESSION['id'];
		$uid=$_SESSION['id'];
		$data['content']=htmlspecialchars($data['content']);
		$kiss='-'.$data['kiss_num'];
		$this->autonum("user","kiss",$kiss,$uid);
		//增加提问
		if ($id=M('question')->add($data)) {
			// $sql="update user set kiss=kiss-{$kiss} where id='{$uid}'";
			// // var_dump($sql);
			// $res=M('user')->execute($sql);			
			$arr = array(
    			'error'=>0,
    			'info'=>'发布成功!',
    			'url'=>U("home/contg/index",array('id' =>$id))
    			);
    		echo json_encode($arr);
    		exit;
		}else{
			$arr = array(
    			'error'=>1,
    			'info'=>'发布失败'
    			);
    		echo json_encode($arr);
    		exit;
		}

	}
	//编辑器上传图片接口
	public function uploadpic()
	{
		$upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     1048576 ;// 设置附件上传大小
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    $upload->rootPath  =     './Uploads/pic/'; // 设置附件上传根目录
	    $upload->savePath  =     ''; // 设置附件上传（子）目录
	    $info   =   $upload->upload();
        $info= current($info);
        $path =__ROOT__.'/Uploads/pic/'.$info['savepath'].$info['savename'];
        
        if(!$info) { //上传失败
 			$arr=array(
 				'code'=>1,
 				'msg'=>'上传失败，图片大小限制1M'
 				);
 			echo json_encode($arr);
 			exit();
        }else{// 上传成功  
        	$arr=array(
        		'code'=> 0,
			  	'msg'=>'上传成功',
			  	'data'=>array(
			  		'src'=>$path,
			    	'title'=>'',
			  		)			    
        		);
        	echo json_encode($arr);
        }
	}
	//编辑提问
	public function editquestion()
	{
		$cate=D('Category')->GetCate();
		$this->assign('cate',$cate);
        $question =D('Verify')->getOneVerify();
        $this->assign('question',$question);
        $id=$_GET['id'];
        $sql="select id,title,content, cid,kiss_num as oldkiss from question where id='{$id}'";
        $res=M('question')->query($sql);
        $res=current($res);
        $this->assign('info',$res);
		$this->display();
	}
	//提交编辑
	public function editsave()
	{
		//POST 	获取人类验证		
		$answer = $_POST['answer'];
		//判断人类验证
		if ($answer!=$_SESSION['answer']) {
			$arr = array(
    			'error'=>1,
    			'info'=>'人类验证失败，请重新输入！'
    			);
    		echo json_encode($arr);
    		exit;
		}

		//获取KISS
		$kiss=$_POST['kiss_num'];
		$id=$_SESSION['id'];		
		$sql="select kiss from user where id='{$id}'";
		$res=M('user')->query($sql);
		$res=current($res);
		$kiss_num=$res['kiss']+$_POST['oldkiss'];
		//判断KISS
		if ($kiss>$kiss_num) {
			$_SESSION['answer'] ='';
			$arr = array(
    			'error'=>1,
    			'info'=>'您的飞吻不够，问题提交失败！'
    			);
    		echo json_encode($arr);
    		exit;
		}

		//获取POST表单所有信息
		$data=$_POST;
		//增加时间
		$qid=$_POST['qid'];
		$data['create_time']=time();
		$data['updata']=time();
		$data['uid']=$_SESSION['id'];
		$uid=$_SESSION['id'];
		$data['content']=htmlspecialchars($data['content']);
		$kiss='-'.$data['kiss_num'];
		$oldkiss='+'.$_POST['oldkiss'];
		//修改提问
		if (M('question')->where("id='{$qid}'")->save($data)) {
			$this->autonum("user","kiss",$oldkiss,$uid);
			$this->autonum("user","kiss",$kiss,$uid);		
			$arr = array(
    			'error'=>0,
    			'info'=>'发布成功!',
    			'url'=>U("home/contg/index",array('id'=>$qid))
    			);
    		echo json_encode($arr);
    		exit;
		}else{
			$arr = array(
    			'error'=>1,
    			'info'=>'发布失败'
    			);
    		echo json_encode($arr);
    		exit;
		}
	}
}